<?php

/*
 * CopyRight  : (C)2012-2099 HaoTeam Inc.
 * Document   : WechatModelTrait.php
 * Created on : 2021-3-22 9:27:00
 * Author     : Tiger <1192851302@qq.com>
 * Description: This is NOT a freeware, use is subject to license terms.
 *              这即使是一个免费软件,使用时也请遵守许可证条款,得到当时人书面许可.
 *              未经书面许可,不得翻版,翻版必究;版权归属 HaoTeam Inc;
 */

namespace haoteam\traits;

/**
 * 微信模型基础
 * Class WechatModelTrait
 * Tiger <1192851302@qq.com> 2021-3-22 9:27:00
 */
trait WechatModelTrait {

    /**
     * 新增前事物
     * @author Tiger <1192851302@qq.com>
     * @param object $model Description
     */
    public static function onBeforeInsert($model) {
        $model->mid = request()->mid();
    }

    /**
     * 更新前事物
     * @author Tiger <1192851302@qq.com>
     * @param object $model Description
     */
    public static function onBeforeUpdate($model) {
        $model->setUpdateWhere(array_merge($model->getWhere() ?: [], ['mid' => request()->mid()]));
    }

    /**
     * 只读取
     * @author Tiger <1192851302@qq.com>
     * @param object $query
     * @param string $alias Description
     */
    public function scopeApply($query, $alias = '') {
        $query->where(($alias ?: '') . 'mid', '=', request()->mid());
    }

}
